﻿using DevExpress.Utils;
using DevMy.PublicSetUp;
using MDIDemo.Model;
using MDIDemo.PublicClass;
using MDIDemo.PublicSetUp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_Site : DevExpress.XtraEditors.XtraForm
    {
        public Form_Site(string FormText)
        {
            InitializeComponent();
            SetComponent(FormText);
        }
        private GridC gridC = new GridC();
        private Class_SQLiteOperator class_SQLiteOperator = new Class_SQLiteOperator();
        private WaitDialogForm waitDialogForm;
        private Class_MySqlDataBase class_MySqlDataBase;
        private Class_LookUpEdit class_LookUpEdit;

        private void SetPicture(string infomation)
        {
            this.barStaticItem1.Caption = infomation;
        }
        private void SetOperate(string infomation)
        {
            this.barStaticItem3.Caption = infomation;
        }
        private void SetXY(Point point)
        {
            this.barStaticItem2.Caption = string.Format("坐标---X:{0},Y:{1}", point.X, point.Y);
        }
        private void SetComponent(string FormText)
        {
            this.cardView1.CardCaptionFormat = "第{0}个未载入点";
            this.cardView1.OptionsBehavior.ReadOnly = true;
            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);

            LookUpEditColumnModel[] lookUpEditColumnModels = new LookUpEditColumnModel[2];
            lookUpEditColumnModels[0] = new LookUpEditColumnModel("projectName", "项目名称");
            lookUpEditColumnModels[1] = new LookUpEditColumnModel("id", "项目编号");

            DataTable dataTable = new DataTable();
            dataTable = class_MySqlDataBase.GetDataTable("SELECT id,projectName FROM vou_project ORDER BY createDate DESC");
            class_LookUpEdit = new Class_LookUpEdit();
            class_LookUpEdit.SetLookUpEdit(this.lookUpEdit1, "projectName", "id", lookUpEditColumnModels, "项目信息");
            this.lookUpEdit1.Properties.DataSource = dataTable;
            dataTable.Dispose();

            this.Text = FormText;
            SetGrid("I0007");

            //dataTable = new DataTable();
            //DataColumn dataColumn = new DataColumn();
            //dataColumn.Caption = "传感器编号";
            //dataColumn.ColumnName = "id";
            //dataColumn.DataType = Type.GetType("System.String");
            //dataTable.Columns.Add(dataColumn);

            //DataColumn dataColumn2 = new DataColumn();
            //dataColumn2.Caption = "设备名称";
            //dataColumn2.ColumnName = "equipmentName";
            //dataColumn2.DataType = Type.GetType("System.String");
            //dataTable.Columns.Add(dataColumn2);
            //for (int i = 0; i < 6; i++)
            //{
            //    DataRow dataRow = dataTable.NewRow();
            //    dataRow[0] = "传感器" + (i + 1);
            //    dataRow[1] = "设备名称1";
            //    dataTable.Rows.Add(dataRow);
            //}
            //this.gridControl1.DataSource = dataTable;

        }
        protected void SetGrid(string GridId)
        {
            //gridC.SetGridC(this.gridControl1
            //    , this.cardView1
            //    , "分组区"
            //    , class_SQLiteOperator.GetGridColumnList(GridId));
        }

        private void panelControl2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void imageSlider1_Click(object sender, EventArgs e)
        {
            string infomation = string.Format("矿道{0}截图展示图", this.imageSlider1.CurrentImageIndex + 1);
            this.SetPicture(infomation);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("这是保存坐标事件");
        }

        private void imageSlider1_MouseDown(object sender, MouseEventArgs e)
        {
        }

        private void imageSlider1_MouseMove(object sender, MouseEventArgs e)
        {
            SetXY(e.Location);
        }

        private void Form_Site_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (waitDialogForm != null)
            {
                waitDialogForm.Dispose();
            }
            class_MySqlDataBase.CloseSqlConnection();
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");
            string mainId = this.lookUpEdit1.EditValue.ToString();

            #region
            //string Sql = this.lookUpEdit1.EditValue.ToString();
            //if (Sql != null && Sql.Length > 0)
            //{
            //    Sql = string.Format(@"SELECT sub.id
            //        ,sub.mainId
            //        ,sub.X
            //        ,sub.Y
            //        ,sub.fileName
            //        ,sub.fileStream
            //        ,sub.sortNo
            //        ,sub.remark
            //        ,count(sor.id) as sorCount
            //        FROM vou_project_test as sub
            //        LEFT JOIN vou_sensorsite as sor on sub.id = sor.mainId
            //        WHERE sub.mainId = '{0}'
            //        AND sub.X + sub.Y = 0
            //        GROUP BY sub.id
            //        ,sub.mainId
            //        ,sub.X
            //        ,sub.Y
            //        ,sub.fileName
            //        ,sub.fileStream
            //        ,sub.sortNo
            //        ,sub.remark
            //        ORDER BY sub.sortNo", Sql);
            //    DataTable dataTable = class_MySqlDataBase.GetDataTable(Sql);
            //    this.gridControl1.DataSource = dataTable;
            //}
            #endregion

            string Sql = string.Format(@"Select
fileStream
From vou_project_picture
Where mainId = '{0}'
Order by sortNo", mainId);

            DataTable dataTable = class_MySqlDataBase.GetDataTable(Sql);
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                this.imageSlider1.Images.Clear();
                foreach (DataRow item in dataTable.Rows)
                {
                    byte[] vs = item["fileStream"] as byte[];
                    this.imageSlider1.Images.Add(ReturnPhoto(vs));
                }
            }
            dataTable.Dispose();
            waitDialogForm.Close();
        }

        private Image ReturnPhoto(byte[] streamByte)
        {
            MemoryStream ms = new MemoryStream(streamByte);
            return Image.FromStream(ms);
        }
    }
}
